import axios from '@/utils/request'
export default {
  Tag: {
    form: {
      fields: [
        {
          property: 'category',
          component: {
            props: ['form', 'property'],
            data() {
              return {
                categories: []
              }
            },
            created() {
              this.axios.get(`/manage/categories`, {
                params: {
                  '@filter': '!entity.getParent() && entity.getType().getSlug() == "ProductCategory" ',
                  '@expands': "['entity.children.type']"
                }
              }).then((res) => {
                this.categories = res.data
              })
            },
            render(h) {
              return (
                <el-select v-model={this.form[this.property]} filterable placeholder='类型'>{
                  this.categories.map(type => {
                    return <el-option-group label={type.name} key={type.id} value={type.id}>
                      {
                        type.children.map(category => {
                          if (category.__metadata.type.__metadata.slug === 'tagCategory') {
                            return <el-option key={category.id} label={category.__toString} value={category.id} />
                          }
                        })
                      }
                    </el-option-group>
                  })
                }</el-select>
              )
            }
          }
        },
        'name',
        {
          property: 'image',
          type: 'image',
          help: '请上传200x200的图片'
        },
        'listOrder'
      ]
    },
    list: {
      query: {
        '@expands': "['entity.category']"
      },
      list_filter: {
        'category.parent.id': () => {
          return axios
            .get('/api/categories',
              { params: { '@filter': '!entity.getParent() && entity.getType().getSlug() == "ProductCategory"'}})
            .then(res =>
              Object.assign(
                { __label: '产品分类' },
                ...res.data.map(v => { return { [v.id]: `${v.name}` } })
              )
            )
        },
        'category.id': () => {
          return axios
            .get('/api/categories',
              { params: { '@filter': 'entity.getParent() && entity.getType().getSlug() == "tagCategory"', '@expands': "['entity.parent']" }})
            .then(res =>
              Object.assign(
                { __label: '标签分类' },
                ...res.data.map(v => { return { [v.id]: `${v.name}(${v.parent.__toString})` } })
              )
            )
        }
      },
      list_display: [
        'id',
        {
          property: 'category',
          label: '分类',
          component: {
            props: ['data'],
            render(h) {
              return (
                <div>
                  {this.data.__metadata.parent.__toString}-{this.data.__toString}
                </div>
              )
            }
          }
        },
        'name',
        {
          property: 'image',
          type: 'image'
        },
        {
          property: 'listOrder',
          editable: true
        }
      ]
    }
  }
}
